package com.ericsson.smart.dao;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.sax.SAXSource;

import org.xml.sax.InputSource;

import com.ericsson.smart.reportObjects.GenerateReportObjects;
import com.report.meta.ReportType;

public class ReportDAO {

	/*
	 * private static final Logger logger = LoggerFactory .getLogger(HomeController.class);
	 */

	Map<String, Object> reportMetaDataObjects = new HashMap<String, Object>();

	public Map<String, Object> populateReportMetaData() {

		try {
			//System.out.println("Inside populateReportMetaData");
			System.out.println("REPORT-META-DATA: creating a Report Objects from xml file");
			JAXBContext context = JAXBContext.newInstance("com.report.meta");
			//System.out.println("creating unmarshaller");
			Unmarshaller unmarshaller = context.createUnmarshaller();
			//System.out.println("creating JAXBElement object");
			JAXBElement<ReportType> r = unmarshaller.unmarshal(
					new SAXSource(new InputSource(GenerateReportObjects.class.getClassLoader()
							.getResourceAsStream("ConsumptionReport.xml"))), ReportType.class);

			//ReportType r1 = r.getValue();
			//System.out.println(r1.getReportDimentions().size());
			//System.out.println(r1.getReportDimentions());
					
			//reportMetaDataObjects.put("ConsumptionReport", r1);
			
			r = unmarshaller.unmarshal(
					new SAXSource(new InputSource(GenerateReportObjects.class.getClassLoader()
							.getResourceAsStream("TimeGeoConsumptionReport.xml"))), ReportType.class);
			reportMetaDataObjects.put(r.getValue().getReportName(),r.getValue());
			
			r = unmarshaller.unmarshal(
					new SAXSource(new InputSource(GenerateReportObjects.class.getClassLoader()
							.getResourceAsStream("AlarmReport.xml"))), ReportType.class);
			reportMetaDataObjects.put(r.getValue().getReportName(),r.getValue());
			
			r = unmarshaller.unmarshal(
					new SAXSource(new InputSource(GenerateReportObjects.class.getClassLoader()
							.getResourceAsStream("TimeSubstationConsumptionReport.xml"))), ReportType.class);
			reportMetaDataObjects.put(r.getValue().getReportName(),r.getValue());

			/*
			 * reportMetaDataObjects.put("1", r1.getReportDimentions().get(0));
			 * reportMetaDataObjects.put("2", r1.getReportDimentions().get(1));
			 * reportMetaDataObjects.put("3",
			 * r1.getReportDimentions().get(0).getSelectionCriteria().get(0));
			 * reportMetaDataObjects.put("4",
			 * r1.getReportDimentions().get(1).getSelectionCriteria().get(0));
			 * reportMetaDataObjects.put("5", r1.getReportDimentions().get(0).getSelectionCriteria
			 * ().get(0).getSelectionConstraint());
			 * System.out.println(r1.getReportDimentions().get(0));
			 */
			// System.out.println(r1.getReportDimentions().get(0).getSelectionCriteria().get(0).getSelectionDisplayType().);

		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return reportMetaDataObjects;
	}
}
